package com.qidi.service.impl;

import com.qidi.mapper.BookDao;
import com.qidi.mapper.impl.BookDaoimpl;
import com.qidi.pojo.Book;
import com.qidi.pojo.Page;
import com.qidi.service.BookService;

import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-09-02-16:49
 */
public class BookServiceimpl implements BookService {
    BookDao bookDao=new BookDaoimpl();

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryAllBook() {
        return bookDao.queryAllBook();
    }

    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Page<Book> page(Integer pageNo, Integer pageSize) {
        Page<Book> bookPage = new Page<>();
        //当前页, 页数据量
        bookPage.setPageSize(pageSize);

        //总记录数
        Integer totalNum = (Integer) bookDao.TotalBooknum();
        bookPage.setTotalRecord(totalNum);
        //总页数
        Integer totalpage=totalNum/pageSize;
        if (totalNum%pageSize>0){
            totalpage++;
        }
        bookPage.setPageTotal(totalpage);

        bookPage.setPageNo(pageNo);
        //获取当前页的book数据
        int begin=(bookPage.getPageNo()-1)*pageSize;
        List<Book> books = bookDao.queryPageBook(begin, pageSize);
        bookPage.setPageData(books);

        return bookPage;
    }

    @Override
    public Page<Book> pagePrice(Integer pageNo, Integer pageSize, int min, int max) {
        Page<Book> bookPage = new Page<>();
        //当前页, 页数据量
        bookPage.setPageSize(pageSize);

        //总记录数
        Integer totalNum = (Integer) bookDao.BooknumByPrice(min,max);
        bookPage.setTotalRecord(totalNum);
        //总页数
        Integer totalpage=totalNum/pageSize;
        if (totalNum%pageSize>0){
            totalpage++;
        }
        bookPage.setPageTotal(totalpage);

        bookPage.setPageNo(pageNo);
        //获取当前页的book数据
        int begin=(bookPage.getPageNo()-1)*pageSize;
        List<Book> books = bookDao.queryPageBookByPrice(begin,pageSize,min,max);
        bookPage.setPageData(books);

        return bookPage;

    }
}
